package com.example.oasystem.controller.notice;

import com.example.oasystem.entity.notice.AoaNoticeListOfChen;
import com.example.oasystem.entity.notice.AoaNoticeUserRelation;
import com.example.oasystem.entity.notice.NoticeDTO;
import com.example.oasystem.entity.page.Page;
import com.example.oasystem.entity.user.AoaUser;
import com.example.oasystem.service.impl.NoticeServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class NoticeController {

    @Autowired
    NoticeServiceImpl noticeService;

    @RequestMapping("/infromlist")
    public String infromlist(Model model, HttpSession session,
                             @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                             @RequestParam(value = "baseKey", defaultValue = "") String baseKey,
                             @RequestParam(value = "pageSize", defaultValue = "10") int pageSize){
        int userId = Math.toIntExact(((AoaUser) session.getAttribute("user")).getUserId());
        PageInfo<NoticeDTO> list = noticeService.findNoticeLimitByText(userId,pageNum, pageSize,baseKey);
        Page page=new Page(list.getSize(), Math.toIntExact(list.getTotal()),list.getPages(),list.getPageNum(),list.isIsFirstPage(),list.isIsLastPage());
        List<NoticeDTO> noticeLimit=list.getList();
        for (NoticeDTO n:noticeLimit){
            int contain = contain(userId, n.getNoticeId());
            n.setContain(contain);
        }
        model.addAttribute("baseKey", baseKey);
        model.addAttribute("page", page);
        model.addAttribute("list",noticeLimit);
        model.addAttribute("url", "infromlistOnSelect");

        return "inform/informlist";
    }
    @RequestMapping("/infromlistOnSelect")
    public String infromlistOnSelect(Model model, HttpSession session,
                             @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                             @RequestParam(value = "baseKey", defaultValue = "") String baseKey,
                             @RequestParam(value = "pageSize", defaultValue = "10") int pageSize){
        int userId = Math.toIntExact(((AoaUser) session.getAttribute("user")).getUserId());
        System.out.println(baseKey);
        PageInfo<NoticeDTO> list = noticeService.findNoticeLimitByText(userId,pageNum, pageSize,baseKey);
        Page page=new Page(list.getSize(), Math.toIntExact(list.getTotal()),list.getPages(),list.getPageNum(),list.isIsFirstPage(),list.isIsLastPage());
        List<NoticeDTO> noticeLimit=list.getList();
        for (NoticeDTO n:noticeLimit){
            int contain = contain(userId, n.getNoticeId());
            n.setContain(contain);
        }
        model.addAttribute("baseKey", baseKey);
        model.addAttribute("page", page);
        model.addAttribute("list",noticeLimit);
        model.addAttribute("url", "infromlistOnSelect");

        return "inform/informlistpaging";
    }
    /**
     * 检查是否转发完成
     * @param uid
     * @param noticeId
     * @return
     */
    private int contain(int uid,int noticeId){
        int count = 1;

        //查找所有的直系下级
        List<AoaUser> users = noticeService.selectSonUser(uid);

        //查找某条通知相关的记录,根据noticeId查询receiver里的记录总数
        List<AoaNoticeUserRelation> relationByNoticeId = noticeService.findRelationByNoticeId(noticeId);
        ArrayList usersId = new ArrayList();

        for (AoaUser u:users) {
            usersId.add(u.getUserId());
        }

        if (users.size()>0) {
            if (relationByNoticeId != null) {

                for (AoaNoticeUserRelation u : relationByNoticeId) {
                    if (usersId.contains(u.getRelatinUserId())) {
                        count = 2;
                    }
                }
                if (count != 2) {
                    count = 3;
                }
            }
        }
        return count;
    }
    @RequestMapping("/informshow")
    public String informshow(@RequestParam(value = "id",defaultValue = "0")int id,
                             Model model,HttpSession session,
                             @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                             @RequestParam(value = "baseKey", defaultValue = "") String baseKey,
                             @RequestParam(value = "read",defaultValue = "-1")int read,
                             @RequestParam(value = "relationid",defaultValue = "0")int relationid){
        AoaUser user = (AoaUser) session.getAttribute("user");
        System.out.println("id"+id+"read"+read+"relationid"+relationid);
        //找出该对象
        AoaNoticeListOfChen noticeList = noticeService.selectNotice(id);
        //修改relation中的已读未读状态
/*        if (relationid ==0 ){

            noticeService.updateRead(id, Math.toIntExact(user.getUserId()));
        }else {*/
            if (read==0){
                noticeService.updateReadByRelation(relationid);
            }
/*        }*/
        model.addAttribute("notice",noticeList);
        model.addAttribute("pageNum",pageNum);
        model.addAttribute("baseKey",baseKey);
        return "inform/informshow";
    }
    @RequestMapping("/forwardother")
    public String forwardother(@RequestParam(value = "noticeId",defaultValue = "0")int noticeId,
                               @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                               @RequestParam(value = "baseKey", defaultValue = "") String baseKey,
                               HttpSession session){
        AoaUser user = (AoaUser) session.getAttribute("user");

        List<AoaUser> users = noticeService.selectSonUser(Math.toIntExact(user.getUserId()));//直系下级
        //根据noticeId查出所有的记录
        List<AoaNoticeUserRelation> relationByNoticeId = noticeService.findRelationByNoticeId(noticeId);

        //进行转发，先将noticeId涉及的记录全部删去(除去登录者记录)
/*        for (AoaUser u:users) {
            for (AoaNoticeUserRelation no:relationByNoticeId){
                if (u.getUserId().equals(no.getRelatinUserId())){
                    noticeService.deleteRelatioByRelationId(Math.toIntExact(no.getRelatinId()));
                }
            }
        }*/


        //再将直系下级的记录进行添加
        for (AoaUser u:users) {
            noticeService.insertNoticeRelation(noticeId,Math.toIntExact(u.getUserId()));
        }


        return "forward:/infromlist?pageNum="+pageNum;
    }
    @RequestMapping("/informlistdelete")
    public String informlistdelete(@RequestParam(value = "id",defaultValue = "0")int id,
                                   @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                   @RequestParam(value = "baseKey", defaultValue = "") String baseKey,
                                   Model model){
        noticeService.deleteRelatioByRelationId(id);
        return "forward:/infromlist?pageNum="+pageNum;

    }
}
